package com.example.demo.controller;

import com.example.demo.model.Blog;
import com.example.demo.model.User;
import com.example.demo.service.BlogService;
import com.example.demo.service.UserService;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private BlogService blogService;
    //用于检测是否已经登陆
    @ResponseBody
    @RequestMapping(value = "/loginin")
    public User alreadyDesign(HttpServletRequest req) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if(session == null) {
            User user = new User();
            return user;
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            //虽然有会话,但是会话里为空
            User user1 = new User();
            return user1;
        }
        return user;
    }

    @RequestMapping(value = "/login")
    public String login(String username, String password,  HttpServletRequest req) {
        if(StringUtils.hasLength(username) || StringUtils.hasLength(password)) {
            User user = userService.login(username,password);
            if(user != null) {
                HttpSession session = req.getSession(true);
                session.setAttribute("user",user);
                return "redirect:/blog_list.html";
            }
        }
        return "登陆失败";
    }

    @RequestMapping(value = "/getuserbyid")
    public User getuserbyid(Integer userId) {
        return userService.getUserById(userId);
    }


    @RequestMapping(value = "/design")
    public String design(String username,String password,HttpServletResponse response) {
        if(userService.reg(username,password) == 1) {
            return "redirect:/blog_login.html";
        }
        return "注册失败";
    }

    @RequestMapping(value = "/logout")
    public void logout(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        if(session == null) {
            resp.getWriter().write("未登录");
        } else {
            session.removeAttribute("user");
            resp.sendRedirect("/blog_login.html");
        }
    }


    //通过id拿到博客信息,然后通过博客信息拿到用户信息
    @ResponseBody
    @RequestMapping("/authorinfo")
    public User getAuthorInfo(HttpServletRequest req,HttpServletResponse resp,Integer id) {
        String param = req.getParameter("blogId");
        if(param == null || "".equals(param)) {
            return null;
        }
        Blog blog = blogService.getBlogById(Integer.parseInt(param));
        if(blog == null) {
            return null;
        }
        User author = userService.getUserById(blog.getUserId());
        if(author == null) {
            return null;
        }
        System.out.println("嘿嘿哈哈或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或或");
        author.setPassword("");
        return author;
    }

}
